<?php
// +----------------------------------------------------------------------

namespace alipay;

use EasyWeChat\Kernel\Exceptions\Exception;

require_once (dirname(__FILE__).'/aop/AopClient.php');
require_once (dirname(__FILE__).'/aop/request/AlipaySystemOauthTokenRequest.php');

/**
 * 支付宝工具类
 * @author Paulxun <paulxun@qq.com>
 */
class Auth
{
	/**
	 * 连接设置
	 * @var string
	 */
	private $aop;
	
	/**
	 * 输出错误信息
	 * @var string
	 */
	private $error = 'success';
	
	//代理授权app_auth_token
	private $app_auth_token;
	
	/**
	 * 构造方法
	 */
	public function __construct($token = ''){
		$this->aop = new \AopClient();
		$config = config('alipay');
		$this->aop->gatewayUrl = $config['gateway'];
        $this->aop->appId = $config['app_id'];
		$this->aop->rsaPrivateKey = $config['private_key'];
		$this->aop->alipayrsaPublicKey = $config['public_key'];
		$this->aop->apiVersion = '1.0';
		$this->aop->signType = 'RSA2';
		$this->aop->postCharset = 'utf-8';
		$this->aop->format = 'json';
		if ($token != ''){
			$this->app_auth_token = $token;
		}
	}
	// user_id
	public function getuserid($code = '')
	{
		$request = new \AlipaySystemOauthTokenRequest();
		$request->setGrantType("authorization_code");
		$request->setCode($code);
        $result = $this->aop->execute ($request);
        if (isset($result->error_response)) {
            dd( $result->error_response);
        }
		$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
		$return = $result->$responseNode;
		if (isset($return->code)){
			if ($return->code != 10000) return false;
		}
		return $return;
	}

	/**
	 * 获取错误信息
	 * @access public
	 * @return mixed
	 */
	public function getError()
	{
	    return $this->error;
	}
}